package com.shse.service.impl;

import com.shse.dao.ArticleDao;
import com.shse.dao.CommentDao;
import com.shse.dao.UserDao;
import com.shse.meta.Article;
import com.shse.meta.Comment;
import com.shse.meta.User;
import com.shse.service.ArticleService;
import com.shse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by shse on 3/12/2016.
 */

@Component("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Article> getAllArticle() {
        List<Article> articles =  articleDao.getAllArticle();
        for(Article article:articles){
            User user = userDao.getUserById(article.getUserId().toString());
            article.setUser(user);
        }
        return articles;
    }

    @Override
    public Article getArticleById(String id) {
        Article article = articleDao.getArticleById(id);

        //获取到user
        Integer userId = article.getUserId();
        User user = userDao.getUserById(userId.toString());
        //获取到comments
        List<Comment> comments = commentDao.getComments(Integer.parseInt(id));

        article.setUser(user);
        article.setComments(comments);

        return article;
    }

    @Override
    public List<Article> getArticlesByAuthor(String author) {
        List<Article> articles = articleDao.getArticlesByAuthor(author);
        for(Article article:articles){
            User user = userDao.getUserById((article.getUserId().toString()));
            article.setUser(user);
        }
        return articles;
    }

    @Override
    public void updateArticle(String id, String title, String content) {
        articleDao.updateArticle(id, title, content);
    }

}
